<script setup>
import { ref } from 'vue'
import { userInfoService,userInfoUpdateService } from '@/api/user.js'
import {ElMessage,ElMessageBox} from 'element-plus'
const info = ref({
    id:0,
    userName:'',
    nickName:'',
    email:''
})
//获取个人信息
const getUserInfo =async ()=>{
    let result =await userInfoService();
    console.log(result);
    console.log(result.data.userName);
    info.value.id = result.data.id;
    info.value.userName = result.data.userName ? result.data.userName : '';
    info.value.nickName = result.data.nickName ? result.data.nickName : '';
    info.value.email = result.data.email;
    console.log(info.value);
}
getUserInfo();
const rules = {
    nickName: [
        { required: true, message: '请输入用户昵称', trigger: 'blur' },
        {
            pattern: /^\S{2,10}$/,
            message: '昵称必须是2-10位的非空字符串',
            trigger: 'blur'
        }
    ],
    email: [
        { required: true, message: '请输入用户邮箱', trigger: 'blur' },
        { type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
    ]
}
const userInfoUpdate = async()=>{
    let result =await userInfoUpdateService(info.value);
    if(result.code==0){
        ElMessage.success("修改成功,刷新页面生效");
    }
    else{
        ElMessage.error(result.message ? result.message :"修改失败");
    }
}
</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>用户基本信息</span>
            </div>
        </template>
        <el-row>
            <el-col :span="12">
                <el-form :model="info" :rules="rules" label-width="100px" size="large">
                    <el-form-item label="用户名">
                        <el-input v-model="info.userName" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="昵称" prop="nickName">
                        <el-input v-model="info.nickName"></el-input>
                    </el-form-item>
                    <el-form-item label="邮箱" prop="email">
                        <el-input v-model="info.email"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="userInfoUpdate">确认修改</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </el-card>
</template>